import requests
import json  # Import json module for pretty printing

# Load the token from token.txt
def load_token():
    with open("token.txt", "r") as file:
        return file.read().strip()

# Base URL for the API
BASE_URL = "http://localhost:8080/api/license-keys"

def test_generate_license_key(product_id, duration):
    url = f"{BASE_URL}/generate"
    payload = {
        "action": "generate",
        "product_id": product_id,
        "duration": duration,
        "amount": 1
    }
    headers = {
        "Authorization": f"Bearer {load_token()}"
    }
    response = requests.post(url, params=payload, headers=headers)
    print("Generate License Key Response:")
    print(json.dumps(response.json(), indent=4))  # Pretty-print JSON response

def test_query_license_keys(product_id, sort):
    url = BASE_URL
    params = {
        "product_id": product_id,
        "sort": sort,
        "rows": 10,
        "page": 20
    }
    headers = {
        "Authorization": f"Bearer {load_token()}"
    }
    response = requests.get(url, params=params, headers=headers)
    print("Query License Keys Response:")
    print(json.dumps(response.json(), indent=4))  # Pretty-print JSON response

if __name__ == "__main__":
    # Test generating a license key
    #test_generate_license_key(product_id=5, duration=3600000)  # Example product_id and duration
    # Test querying license keys
    test_query_license_keys(product_id=5, sort="created_at")  # Example product_id and sort
